package com.dao.impl;

import com.dao.UserDao;
import com.pojo.Tour;
import com.pojo.User;

import java.util.List;

/**
 * @Author hjb
 * @Date 7/6/2021 下午 8:22
 * @Version 1.0
 * @Contact 1754729389@qq.com
 */
public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select * " +
                "from users";
        return Integer.parseInt(queryForSingleValue(sql).toString());
    }

    @Override
    public List<User> queryForPageItems(int beginNo, int pageSize) {
        String sql = "select user_id id,user_name username,user_password password,user_address address," +
                "user_email email,user_phone phone,user_identity identity" +
                " from users " +
                "limit ?,?";
        return queryForList(User.class,sql,beginNo,pageSize);
    }

    @Override
    public User queryUserByUserName(String username) {
        String sql = "select user_id id,user_name username,user_password password,user_address address," +
                "user_email email,user_phone phone,user_identity identity" +
                " from users where user_name=?";
        return queryForOne(User.class,sql,username);
    }

    @Override
    public User queryUserByNameAndPassword(String username, String password) {
        String sql = "select user_id id,user_name username,user_password password,user_address address," +
                "user_email email,user_phone phone,user_identity identity" +
                " from users where user_name=? " +
                "and user_password=?";
        return queryForOne(User.class,sql,username,password);
    }

    @Override
    public int saveUser(User user) {

        String sql = "insert into users(user_name,user_password,user_address,user_email,user_phone,user_identity," +
                "user_code,user_deposit,user_balance) " +
                "values(?,?,?,?,?,?,?,?,?)";
        String sql1 = "INSERT INTO users(user_name,user_password,user_address,user_email,user_phone,user_identity)\n" +
                "VALUES(?,?,?,?,?,?)";
        return update(sql1,user.getUsername(),user.getPassword(),user.getAddress(),user.getEmail(),user.getPhone(),user.getIdentity());
    }

    @Override
    public int deleteUser(Integer id) {
        String sql = "delete " +
                "from users" +
                " where user_id=?";
        return update(sql,id);
    }

    @Override
    public User queryUserById(Integer id) {
        String sql = "select user_id id,user_name username,user_password password,user_address address," +
                "user_email email,user_phone phone,user_identity identity" +
                " from users " +
                "where user_id=?";
        return queryForOne(User.class,sql,id);
    }

    @Override
    public int updateUser(User user) {
        String sql = "update users " +
                "set user_name=?,user_password=?," +
                "user_address=?,user_email=?," +
                "user_phone=?,user_identity=? " +
                "where user_id=?";
        return update(sql,user.getUsername(),user.getPassword(),user.getAddress(),user.getEmail(),user.getPhone(),user.getIdentity(),user.getId());
    }


}
